/**
 *
 *  @author ala
 *  @date 2024-09-20 17:01
 */
package tools

import "container/heap"

/**
 *	堆中的元素
 */
type Item struct {
	//	堆中的值
	val int
	//	优先级
	priority int
	//	元素下标
	idx int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int {
	return len(pq)
}

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].idx, pq[j].idx = j, i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.idx = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil
	item.idx = -1
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(item *Item, val, priority int) interface{} {
	heap.Fix()
}
